const { HttpException } = require('../core/HttpException')
const catchError = async (ctx, next) => {
  try {
    await next()
  } catch (error) {
    if (error instanceof HttpException) {
      ctx.body = {
        msg: error.message,
        error_code: error.errorCode,
        request: `${ctx.method}-${ctx.path}`
      }
      ctx.status = error.code
    } else {
      ctx.body = {
        msg: '发生了未知错误',
        error_code: 999,
        request: `${ctx.method}-${ctx.path}`
      }
    }
  }
}
module.exports = catchError